<?php
header("Content-type:text/html; charset=utf-8"); 
date_default_timezone_set('PRC'); 
/**
 * http接口调用入口文件
 */
 
// 当前目录
if (!defined("ZJH_DIR")) {
	define("ZJH_DIR", dirname(__FILE__));	
}
	
// 包含头文件
function path_format($file) {
	return ZJH_DIR . '/' . $file;
}

//自动加载
include_once(path_format('lib/function.php'));
set_include_path(get_include_path() . PATH_SEPARATOR . ZJH_DIR.'/lib/'. PATH_SEPARATOR .ZJH_DIR.'/config/');
spl_autoload_register("autoload");

	
/*
	include_once(path_format('lib/logger.php'));
	include_once(path_format('lib/apibase.php'));
	include_once(path_format('lib/function.php'));
	include_once(path_format('lib/mysql.php'));
	include_once(path_format('lib/httpclient.php'));
	include_once(path_format('config/config.php'));
	include_once(path_format('config/game.php'));
	include_once(path_format('phpqrcode/phpqrcode.php'));
*/
	//include_once(path_format('config/language.php'));
	//exit;
	/*if(isset($_GET['param'])){
	    echo json_encode();	
	}*/
	//$mysql = new Mysql();
	//var_dump($mysql);
	//exit;
    //$param = $_GET['param'];
	//$pwd密钥 $data需加密字符串
	
	
	//var_dump($_REQUEST['base']);
	//$req = "iH+mgMN71Cc0/+J08WZeuocGLO5VbgSyiT78rs+se56MSHpNdFlKaZMy3s9RaVQeTS01/RsDJ2U19kJAZb+iElK/HN891639YFY4pwSj0DLd7KGSTgbZH7FKaH+/bsiBcziDqI1VrF7rV8sX8ZX/cIdJhxwWZtJA8Q==";
	/*$req = "6RzS6awV6W5alpY3kAU236FwSZxoXyqHpw/a3q7eGvOxM1g7ETc/DLEI7+NzCDpwIlhbnn5uQgtB1HhxSZ3FfT3bb/0H5oHfBz9e0yaZ4R7/jdbzPGL7JYBmShLeHLn0Fl2hkrx5jj2EPqVjkOaUHO468z4sV/5igX8dgAULay0bgscRuO5GySpvTVNgFXH3NA==";
	$ddata = base64_decode($req);
	var_dump($ddata);
	$data = rc4("000000", $ddata);
	
	echo $data;
	exit;*/
	//$data = str_replace(array('+','/','='),array('-','_',''),$data);
	
	
	if(!isset($_GET['base'])){
	   echo json_encode(array('ret' => 1, "desc" => '请求参数有误.'));
	   exit(1); 	
	}
	
	//$base = str_replace(array('-'),array('+'),$_GET['base']);
	//$base = base64_decode($base);
	
	//$data = rc4('YUS!8W@zyl@k9NrQ',$base);
	//file_put_contents('api.txt',$data."\n",FILE_APPEND);
	//
	//$test = array("action"=>"InitCache","ver"=>"1.5.1","param"=>array("venue"=>1,"announcement"=>1));
    //$data = str_replace('=',':',$data);
	
	$data = json_decode($_GET['base'],true);


	unset($_GET['base']);
	
	if(!empty($data)){
	  foreach($data as $key => $val){
		  //var_dump($val);
		   if(is_array($val)){
			   
			  $_GET[$key]=$val;
		
			  
			}
			 $_GET[$key]=$val;
	  }
	}
	//
	//file_put_contents('api.txt',json_encode($_GET['param'])."\n",FILE_APPEND);
	
	if(isset($_GET['param']) || !empty($_GET['param']))
	$_GET['param']=json_encode($_GET['param']);
	//action=InitCache&ver=1.5.1&param={"venue":1,"announcement":1,"goods":1,"gift":1,"award":1,"marquee":1,"cointasklist":1,"pass":"zjhcache","all":1}
	//array("action"=>"InitCache","ver"=>"1.5.1","param"=>array("venue"=>1,"announcement"=>1));
	//echo json_encode(array("action"=>"InitCache","ver"=>"1.5.1","param"=>array("venue"=>1,"announcement"=>1)));
	//exit;
	//var_dump($_GET['param']);
	$class_name = null;
	if (isset($_GET['cmd'])) {	//目前没用cmd,全部改用action

		$class_name = Cmd::$cmd_array[$_GET['cmd']];
		echo json_encode(array('ret'=>100,"desc"=>"cmd = " . $_GET['cmd']));

	} elseif (isset($_GET['action'])) {

		$class_name = $_GET['action'];
//		file_put_contents('a.txt',$class_name."\n",FILE_APPEND);

	} else {
		echo json_encode(array('ret' => 2, "desc" => '请求参数有误.'));
		exit(1);

	}

	$file = path_format("interface/" . $class_name . ".php");

	if (isset($_GET['ver'])) {
		
		$file = path_format("interface/" . $_GET['ver'] . '/' .  $class_name . ".php");
	}
    
	if (file_exists($file)) {
		
		include_once($file);
		$cmd = new $class_name;
		$cmd->exec_logic();

	} else {
		echo json_encode(array('ret' => 3, "desc" => ''.$class_name.' no found.'));
		exit(2);
	}
	
?>
